/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestor.GUI;

import gestor.listModel.LMPrivilegio;
import gestor.modelo.MySql;
import gestor.modelo.Privilegio;
import gestor.modelo.Usuario;
import gestor.modelo.bd.BD;
import gestor.modelo.bd.Tabla;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Patricio Pérez Pinto
 */
public class GUI extends javax.swing.JFrame {
    private MySql mySql;

    /**
     * Creates new form GUI
     */
    public GUI() throws ClassNotFoundException, SQLException {
        initComponents();
        String rootpass = JOptionPane.showInputDialog("Root password: ");
        if(rootpass == null){
            System.exit(0);
        }
        
        mySql = new MySql(rootpass);
        listaBD.setModel(mySql);
        listaBD.setSelectedIndex(0);
        cargarTablas();
        listaPrivilegios.setModel(new LMPrivilegio());
        listaPrivilegios.setSelectedIndex(0);
        ponerPrivilegiosEnLabel();
        llenarComboUsuarios();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listaBD = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaTablas = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        listaPrivilegios = new javax.swing.JList();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        btnProcesar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        nombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        pass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        host = new javax.swing.JComboBox();
        chkSoloCrearUsuario = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        lblBD = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblTabla = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblPrivilegios = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        cboUsuarios = new javax.swing.JComboBox();
        passNueva = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        lblBD1 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblTabla1 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        lblPrivilegios1 = new javax.swing.JLabel();
        nombreNuevo = new javax.swing.JTextField();
        chkNombreNuevo = new javax.swing.JCheckBox();
        chkPassNueva = new javax.swing.JCheckBox();
        chkHostNuevo = new javax.swing.JCheckBox();
        chkPrivilegios = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        hostNuevo = new javax.swing.JTextField();
        chkSobreescribirPrivilegios = new javax.swing.JCheckBox();
        btnEliminarPrivilegios = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        listaBD.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listaBD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                listaBDMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(listaBD);

        listaTablas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                listaTablasMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(listaTablas);

        listaPrivilegios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                listaPrivilegiosMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(listaPrivilegios);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuario"));

        btnProcesar.setText("Crear usuario");
        btnProcesar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcesarActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre:");

        jLabel2.setText("Password:");

        jLabel5.setText("Host:");

        host.setEditable(true);
        host.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "localhost", "%" }));

        chkSoloCrearUsuario.setText("Sólo crear usuario, sin privilegios ni base de dato");

        jLabel3.setText("Base de datos:");

        jLabel4.setText("Tabla:");

        jLabel6.setText("Privilegios:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(387, 387, 387))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblTabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(host, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(lblBD, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(chkSoloCrearUsuario))))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(pass))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblPrivilegios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnProcesar)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(host, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkSoloCrearUsuario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblBD, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTabla))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPrivilegios, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProcesar)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblBD, lblTabla, pass});

        jTabbedPane1.addTab("Crear", jPanel2);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuario"));

        jLabel7.setText("Nombre:");

        cboUsuarios.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboUsuariosItemStateChanged(evt);
            }
        });
        cboUsuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboUsuariosActionPerformed(evt);
            }
        });
        cboUsuarios.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                cboUsuariosPropertyChange(evt);
            }
        });
        cboUsuarios.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cboUsuariosKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cboUsuariosKeyTyped(evt);
            }
        });

        passNueva.setEditable(false);

        jLabel10.setText("Base de datos:");

        jLabel11.setText("Tabla:");

        jLabel12.setText("Privilegios:");

        nombreNuevo.setEditable(false);

        chkNombreNuevo.setText("Cambiar nombre:");
        chkNombreNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkNombreNuevoActionPerformed(evt);
            }
        });

        chkPassNueva.setText("Cambiar pass:");
        chkPassNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkPassNuevaActionPerformed(evt);
            }
        });

        chkHostNuevo.setText("Cambiar host");
        chkHostNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkHostNuevoActionPerformed(evt);
            }
        });

        chkPrivilegios.setText("Cambiar privilegios");
        chkPrivilegios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkPrivilegiosActionPerformed(evt);
            }
        });

        jButton1.setText("Actualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        hostNuevo.setEditable(false);

        chkSobreescribirPrivilegios.setText("Sobre escribir privilegios");
        chkSobreescribirPrivilegios.setEnabled(false);

        btnEliminarPrivilegios.setText("Eliminar todos los permisos");
        btnEliminarPrivilegios.setEnabled(false);
        btnEliminarPrivilegios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarPrivilegiosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(0, 713, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(chkPrivilegios)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(chkSobreescribirPrivilegios)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEliminarPrivilegios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(10, 10, 10)
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(lblTabla1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                                        .addComponent(lblBD1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(lblPrivilegios1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(cboUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(chkNombreNuevo)
                                        .addComponent(chkPassNueva)
                                        .addComponent(chkHostNuevo))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(passNueva, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(hostNuevo, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                            .addComponent(nombreNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                                            .addGap(2, 2, 2))))))
                        .addContainerGap(364, Short.MAX_VALUE))))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {chkHostNuevo, chkNombreNuevo, chkPassNueva, jLabel10, jLabel11, jLabel12, jLabel7});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkNombreNuevo)
                    .addComponent(nombreNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkPassNueva))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkHostNuevo)
                    .addComponent(hostNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkPrivilegios)
                    .addComponent(chkSobreescribirPrivilegios)
                    .addComponent(btnEliminarPrivilegios))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblBD1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTabla1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblPrivilegios1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel10, jLabel11, jLabel12, jLabel7});

        jTabbedPane1.addTab("Actualizar", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void listaBDMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaBDMouseReleased
        cargarTablas();
    }//GEN-LAST:event_listaBDMouseReleased

    private void listaTablasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaTablasMouseReleased
        List<Tabla> selectedValuesList = listaTablas.getSelectedValuesList();
        String s = "";
        for(Tabla t : selectedValuesList){
            s+=(t.getNombre().equalsIgnoreCase("*") ?"Todas":t.getNombre())+", ";
            if(t.getNombre().equalsIgnoreCase("*")){
                break;
            }
        }
        s = s.substring(0, s.length() - 2);
        lblTabla.setText(s);
        lblTabla1.setText(s);
    }//GEN-LAST:event_listaTablasMouseReleased

    private void listaPrivilegiosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaPrivilegiosMouseReleased
        ponerPrivilegiosEnLabel();
    }//GEN-LAST:event_listaPrivilegiosMouseReleased

    private void btnProcesarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcesarActionPerformed
        try {
            Usuario u = new Usuario(nombre.getText().trim(), pass.getText(), host.getSelectedItem().toString());
               if (chkSoloCrearUsuario.isSelected()) {
                   mySql.crearUsuario(u);
               } else {
                   mySql.otorgarPrivilegios(
                           listaPrivilegios.getSelectedValuesList(),
                           u,
                           (BD) listaBD.getSelectedValue(),
                           listaTablas.getSelectedValuesList());
               }
               llenarComboUsuarios();
               JOptionPane.showMessageDialog(this, "Usuario creado");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage());
        }
    }//GEN-LAST:event_btnProcesarActionPerformed

    private void cboUsuariosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboUsuariosItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cboUsuariosItemStateChanged

    private void cboUsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboUsuariosActionPerformed
        Usuario u = (Usuario)cboUsuarios.getSelectedItem();
        if(u != null){
            nombreNuevo.setText(u.getNombre());
            passNueva.setText(u.getPass());
            hostNuevo.setText(u.getHost());
        }
    }//GEN-LAST:event_cboUsuariosActionPerformed

    private void cboUsuariosPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cboUsuariosPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_cboUsuariosPropertyChange

    private void cboUsuariosKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cboUsuariosKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_cboUsuariosKeyReleased

    private void cboUsuariosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cboUsuariosKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_cboUsuariosKeyTyped

    private void chkNombreNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkNombreNuevoActionPerformed
        nombreNuevo.setEditable(chkNombreNuevo.isSelected());
    }//GEN-LAST:event_chkNombreNuevoActionPerformed

    private void chkPassNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkPassNuevaActionPerformed
        passNueva.setEditable(chkPassNueva.isSelected());
    }//GEN-LAST:event_chkPassNuevaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Object usuario = cboUsuarios.getSelectedItem();
         if(JOptionPane.showConfirmDialog(this, "¿Realmente desea actualizar?") == JOptionPane.YES_OPTION){
            try {
                Usuario uNuevo = getNuevoUsuario();
                Usuario uAntiguo = (Usuario)usuario;
                mySql.actualizarUsuario(uAntiguo, uNuevo);
                
                if(chkPrivilegios.isSelected()){
                    mySql.actualizarPrivilegios(
                           listaPrivilegios.getSelectedValuesList(),
                           uNuevo,
                           (BD) listaBD.getSelectedValue(),
                           listaTablas.getSelectedValuesList(), 
                           chkSobreescribirPrivilegios.isSelected());
                }
                llenarComboUsuarios();
                JOptionPane.showMessageDialog(this, "Usuario actualizado");
            } catch (SQLException ex) {
                Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage());
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void chkHostNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkHostNuevoActionPerformed
        hostNuevo.setEnabled(chkHostNuevo.isSelected());
    }//GEN-LAST:event_chkHostNuevoActionPerformed

    private void chkPrivilegiosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkPrivilegiosActionPerformed
        this.chkSobreescribirPrivilegios.setEnabled(chkPrivilegios.isSelected());
        btnEliminarPrivilegios.setEnabled(chkPrivilegios.isSelected());
    }//GEN-LAST:event_chkPrivilegiosActionPerformed

    private void btnEliminarPrivilegiosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarPrivilegiosActionPerformed
       
        if(JOptionPane.showConfirmDialog(this, "¿Realmente desea eliminar los privilegios del usuario seleccionado?") == JOptionPane.YES_OPTION){
            try {
                Usuario u = (Usuario)cboUsuarios.getSelectedItem();
                mySql.quitarTodosLosPrivilegio(u);
                JOptionPane.showMessageDialog(this, "Se ha eliminado todos los privilegios al usuario");
            } catch (SQLException ex) {
                Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Error: "+ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnEliminarPrivilegiosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new GUI().setVisible(true);
                } catch (        ClassNotFoundException | SQLException ex) {
                    JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminarPrivilegios;
    private javax.swing.JButton btnProcesar;
    private javax.swing.JComboBox cboUsuarios;
    private javax.swing.JCheckBox chkHostNuevo;
    private javax.swing.JCheckBox chkNombreNuevo;
    private javax.swing.JCheckBox chkPassNueva;
    private javax.swing.JCheckBox chkPrivilegios;
    private javax.swing.JCheckBox chkSobreescribirPrivilegios;
    private javax.swing.JCheckBox chkSoloCrearUsuario;
    private javax.swing.JComboBox host;
    private javax.swing.JTextField hostNuevo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblBD;
    private javax.swing.JLabel lblBD1;
    private javax.swing.JLabel lblPrivilegios;
    private javax.swing.JLabel lblPrivilegios1;
    private javax.swing.JLabel lblTabla;
    private javax.swing.JLabel lblTabla1;
    private javax.swing.JList listaBD;
    private javax.swing.JList listaPrivilegios;
    private javax.swing.JList listaTablas;
    private javax.swing.JTextField nombre;
    private javax.swing.JTextField nombreNuevo;
    private javax.swing.JPasswordField pass;
    private javax.swing.JPasswordField passNueva;
    // End of variables declaration//GEN-END:variables

    private Usuario getNuevoUsuario() {
        Usuario u;
        
        Usuario antiguo = (Usuario)cboUsuarios.getSelectedItem();
        u = new Usuario(antiguo.getNombre(), antiguo.getPass(), antiguo.getHost());
        if(chkNombreNuevo.isSelected()){
            u.setNombre(nombreNuevo.getText().trim());
        }
        if(chkPassNueva.isSelected()){
            u.setPass(passNueva.getText().trim());
        }
        if(chkHostNuevo.isSelected()){
            u.setHost(hostNuevo.getText().trim());
        }
        return u;
    }

    private void cargarTablas() {
        BD bd = (BD)listaBD.getSelectedValue();
        listaTablas.setModel(bd);
        listaTablas.setSelectedIndex(0);
        if(bd.getNombre().equalsIgnoreCase("*")){
            lblBD.setText("Todas");
            lblTabla.setText("Todas");
            lblBD1.setText("Todas");
            lblTabla1.setText("Todas");
        }else{
            lblBD.setText(bd.getNombre());
            lblTabla.setText("");
            lblBD1.setText(bd.getNombre());
            lblTabla1.setText("");
        }
    }

    private void ponerPrivilegiosEnLabel() {
        List<Privilegio> selectedValuesList = listaPrivilegios.getSelectedValuesList();
        String s = "";
        for(Privilegio p : selectedValuesList){
            s+=(p.name().equalsIgnoreCase("ALL") ?"Todos":p.name())+", ";
            if(p.name().equalsIgnoreCase("ALL")){
                break;
            }
        }
        s = s.substring(0, s.length() - 2);
        lblPrivilegios.setText(s);
        lblPrivilegios1.setText(s);
    }

    private void llenarComboUsuarios() throws SQLException {
        cboUsuarios.removeAllItems();
        for(Usuario u:mySql.getUsuarios()){
            cboUsuarios.addItem(u);
        }
    }
}
